<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Cash;
use App\Admin\Models\Users;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use function foo\func;
class CashController extends AdminController
{
    protected $title = '提现管理';

    protected function grid()
    {
        $grid = new Grid(new Cash);
        $grid->disableCreateButton();
        $grid->disableExport();


        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) {
                $filter->equal('txnum','提现编号');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('real_name','用户名');
            });

            $filter->column(1/3, function ($filter) {
                $filter->equal('status','提现状态')->select([
                    1 => '审核中',
                    2 => '已到账',
                    3 => '已拒绝',
                ]);
            });
        });
        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $page = request('page');
            if($page){
                $page = ($page-1)*10;
            }
            $row->column('number', $page +$number+1);
        });
        $grid->column('txnum','提现编号');
        $grid->column('real_name','用户名');
        $grid->column('price','提现金额');
        $grid->column('status','提现状态')->using([
            1 => '审核中',
            2 => '已到账',
            3 => '已拒绝',
        ])->dot([
            1 => 'info',
            2 => 'success',
            3 => 'danger',
        ]);
        $grid->column('bank_card','银行卡号');
        $grid->column('bank_open','开户行');
        $grid->column('bank_type','银行');
        $grid->column('reason','拒绝原因');
        $grid->column('price', '提现统计')->sortable()->totalRow(function ($price) {
            return "<span class='text-danger text-bold'><i class='fa fa-yen'></i>{$price} 元</span>";
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
            if($actions->row->status != 1){
                $actions->disableEdit();
            }
        });
        return $grid;
    }

    protected function form(){
        $form = new Form(new Cash);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->text('real_name','真实姓名')->disable();
        $form->text('price','提现金额')->width(3)->disable();
        $form->radio('status','提现状态')->options(['2'=> '已到账', '3'=> '已拒绝' ])->default('1');
        $form->textarea('reason','拒绝备注')->placeholder('如果拒绝提现, 请填写拒绝原因...');
        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $cashinfo = Cash::whereId($id)->first();
            if($form->status == 3){
                //提现拒绝  用户余额返还
                if(empty($form->model()->reason)){
                    return back()->with(admin_toastr('请填写拒绝原因', 'error'));
                }
                $money = $cashinfo['price'];
                $res = DB::table('users')->whereId($cashinfo['user_id'])->increment('cash_money',$money);
                if(!$res){
                    return back()->with(admin_toastr('账户余额返还失败', 'error'));
                }
            }
        });
        return $form;
    }
}
